package com.example.siyun.controller.coursecontrooller;

import com.example.siyun.pojo.*;
import com.example.siyun.service.courseauditservice.CourseAuitService;
import com.example.siyun.service.courseservice.TableService;
import com.example.siyun.util.PageResult;
import com.example.siyun.util.WebUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.*;
/**
 * @Auther: 全
 * @Date: 2022/9/26 09:33
 * @Description: 课程表的控制层
 */
@Slf4j
@RestController
public class CourseTableController {
    @Autowired
    private TableService tableService;
    @Autowired
    private CourseAuitService courseAuitService;
    /**
     * 获取课程列表数据
     * @return 课程列表数据
     */
    @RequestMapping("/advanced/gettable")
    public PageResult getAll(CourseTable coursetable, int page, int limit) {
        log.info("获取课程列表数据");
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(page, limit);
        List<CourseTable> list = tableService.findTable(coursetable);
        PageInfo<CourseTable> pageInfo = new PageInfo<>(list);
        PageResult pageResult=new PageResult();
        pageResult.setState(0);
        pageResult.setMessage("成功");
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setData(pageInfo.getList());
        log.info("获取课程列表数据成功");
        return pageResult;
    }
    /**
     * 获取下拉菜单数据
     * @return 返回下拉菜单的数据
     */
    @RequestMapping("/advanced/getxialatree")
    public Map<String, Object> findMenu(){
        log.info("获取下拉菜单数据");
        Map<String, Object> map = new HashMap<>();
        Map aa=new HashMap();
        List<XiaTree> data = tableService.getXiaLa(0);
        map.put("data",data);
        log.info("获取下拉菜单数据成功");
        return map;
    }
    /**
     * 通过id更新id的状态
     * @param id 要更新的课程id status要更新的状态
     */
    @RequestMapping("/advanced/updateswitch")
    public PageResult updateSwitch(int id,int status){
        log.info("通过id更新课程的状态");
        PageResult pageResult=new PageResult();
        tableService.updateSwitch(id,status);
//        pageResult.setStatus(200);
        log.info("通过id更新课程的状态成功");
        return pageResult;
    }
    /**
     * 通过id查询对应的课程信息
     * @param id 要查询的课程id
     * @return 返回查询出来的课程信息
     */
    @RequestMapping("/advanced/selecttable")
    public PageResult selectTable(int id){
        log.info("通过id查询对应的课程信息");
        CourseTable coursetable=new CourseTable();
        coursetable=tableService.selectTable(id);
        PageResult pageResult=new PageResult();
//        pageResult.setStatus(200);
        pageResult.setData(coursetable);
        log.info("通过id查询对应的课程信息成功");
        return pageResult;
    }
    @RequestMapping("/advanced/addcoursetable")
    public PageResult addCourseTable(CourseTable coursetable){
        CourseAudit audit=new CourseAudit();
        String name = (String) WebUtils.getSession().getAttribute("usernamename");
        Timestamp time2 = new Timestamp(new Date().getTime());
            coursetable.setSale(0);
            coursetable.setIsdel(0);
            coursetable.setStatus(0);
            coursetable.setScname(name);
            coursetable.setAppstatus(0);
            coursetable.setUploadTime(time2);
            PageResult pageResult=new PageResult();
            tableService.addCourseTable(coursetable);
            audit.setAid(coursetable.getId());
            audit.setAuditors(name);
            audit.setAudittime(time2);
            audit.setAuditors("系统");
            audit.setAuditresults("系统已通过");
            audit.setFeedbackdetails("系统审核通过");
            courseAuitService.courseAuditAdd(audit);
//            pageResult.setStatus(200);
            return pageResult;
        }
    /**
     * 根据id删除对应的课程信息
     * @param id 要删除的要删除课程的id
     */
    @RequestMapping("/advanced/delcoursetable")
    public PageResult delCourseTable(int id){
        log.info("根据id删除对应的课程信息");
        tableService.delCourseTable(id);
        PageResult pageResult=new PageResult();
//        pageResult.setStatus(200);
        log.info("根据id删除对应的课程信息成功");
        return pageResult;
    }
    @RequestMapping("/advanced/editcoursetable")
    public PageResult editCourseTable(CourseTable coursetable){
        int cid=coursetable.getId();
        tableService.getCoursePrice(cid,coursetable);
        PageResult pageResult=new PageResult();
//        pageResult.setStatus(200);
        return pageResult;
    }
    /**
     * 通过id查询对应的课程日志
     * @param id 要查询的课程日志数据
     */
    @RequestMapping("/advanced/loginlog")
    public Map<String, Object> loginLog(Integer page, Integer limit,int id) {
        log.info("通过id查询对应的课程日志");
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(page, limit);
        List<CourseLog> list = tableService.courseLog(id);
        PageInfo<CourseLog> pageInfo = new PageInfo<>(list);
        map.put("code", 0);
        map.put("msg", "成功");
        map.put("count", pageInfo.getTotal());
        map.put("data", pageInfo.getList());
        log.info("通过id查询对应的课程日志成功");
        return map;
    }
    @RequestMapping("/advanced/selectallheader")
    public Map<String, Object> selectAllHeader() {
        Map<String, Object> selectdata = tableService.selectData();
        return selectdata;
    }
}
